<?php
/**
 * 简易版品牌咨询管理
 * @author Ice <iceinto@mallog.com.cn>
 */
class BrandNewsModel extends BaseModel
{
	protected $_type = 'new';

	/**
	 * 获取资讯信息
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function getIndexData($field = null)
	{
		$map = $this->getStartToEndTime();
		$field = $field ? $field : 'm_brand_news.id,title,m_brand_news.image,mid,fid,b.id as bid,keyword,b.name';
		$list = $this->where($map)
		->field($field)
		->join('INNER JOIN m_brand as b ON m_brand_news.bid = b.id')
		->limit('0,12')
		->order('rand()')
		->select();
		foreach ($list as $key => $itme) {
			$list[$key]['dis_url'] = $this->getAdUrl($itme['mid'], $itme['fid'], $itme['bid']);
		}
		return $list;
	}

	/**
	 * 获取资讯中的广告信息
	 * @param int $count
	 * @param int $mid
	 * @param int $fid
	 * @param int $bid
	 * @param blooean $isRand
	 * @param string $order
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function getListAd($count = 8, $mid = 0, $fid = 0, $bid = 0, $isImg = true, $order = 'rand(),id')
	{
		$map = array();
		$map = $this->getMap($mid, $fid, $bid);
		$map = array_merge($map, $this->getStartToEndTime());
		$field = 'id,title,image,mid,fid,bid,keyword';
		$list = $this->where($map)->field($field)->limit('0,' . $count)->group('mid,fid,bid')->order($order)->select();
		if ($list) {
			foreach ($list as $key => $itme) {
				$admfb = $this->getAdMFB(0, $itme['fid'], $itme['bid']);//获取品牌信息
				if(empty($admfb)){
					unset($list[$key]);
				}else{
					if ($isImg) {
						$list[$key]['image_url'] = $this->getManagedUrl($itme['image'], $this->_type, true);
					}
					$list[$key]['ad_url'] = $this->getAdUrl($itme['mid'], $itme['fid'], $itme['bid']);
					//合并数据
					$list[$key] = array_merge($list[$key], $admfb);
				}
			}
		}
		return $list;
	}

	/**
	 * 资讯列表图片和广位置获取
	 * @param array $list
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function getImageUrl(&$list)
	{
		if ($list) {
			foreach ($list as $key => $itme) {
				$list[$key]['image_url'] = $this->getManagedUrl($itme['image'], $this->_type, true);
				$list[$key]['ad_url'] = $this->getAdUrl($itme['mid'], $itme['fid'], $itme['bid']);
			}
		}
	}

	/**
	 * 获取单个资讯信息
	 * @param int $mid
	 * @param int $fid
	 * @param int $bid
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function getOneNew($mid, $fid, $bid, $field = 'id,title,image,mid,fid,bid,keyword,start_time,end_time')
	{
		$map = array();
		$map = $this->getMap($mid, $fid, $bid);
		$map = array_merge($map, $this->getStartToEndTime());
		return $this->getBrandNew($map, $field);
	}

	/**
	 * 获取单个资讯信息 不管不不过期
	 * @param $mid
	 * @param $fid
	 * @param $bid
	 * @param string $field
	 * @return mixed
	 */
	public function getOneNewAll($mid, $fid, $bid, $field = 'id,title,image,mid,fid,bid,keyword,start_time,end_time')
	{
		$map = array();
		$map = $this->getMap($mid, $fid, $bid);
		return $this->getBrandNew($map, $field, 'end_time DESC');
	}

	/**
	 * 根据条件获取资讯信息
	 * @param array $map
	 * @param string $field
	 * @param string $limit
	 * @param string $order
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	protected function getBrandNews($map = null, $field = null, $limit = null, $order = null)
	{
		$field = $field ? $field : 'id,title,image,mid,fid,bid,keyword';
		return $this->where($map)->field($field)->limit($limit)->order($order)->select();
	}

	/**
	 * 根据条件获取单个资讯信息
	 * @param array $map
	 * @param string $field
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	protected function getBrandNew($map = null, $field = null, $order = 'id DESC')
	{
		$field = $field ? $field : 'id,title,image,mid,fid,bid,keyword';
		return $this->where($map)->field($field)->order($order)->find();
	}

	/**
	 * 获取当前商场活动的资讯数量
	 * @param int $mid
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function getMallNewCount($mid)
	{
		$map = array();
		$map['mid'] = $mid;
		$map = array_merge($map, $this->getStartToEndTime());
		return $this->where($map)->count();
	}

	/**
	 * 获取查询条件
	 * @param int $mid
	 * @param int $fid
	 * @param int $bid
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function getMap($mid, $fid, $bid)
	{
		$map = array();
		if ($mid) {
			$map['mid'] = $mid;
		}
		if ($fid) {
			$map['fid'] = $fid;
		}
		if ($bid) {
			$map['bid'] = $bid;
		}
		return $map;
	}

	/**
	 * 获取结束时间
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function getStartToEndTime()
	{
		$map = array();
		$nowTime = $this->getThisDayTime();
		$map['end_time'] = array('egt', $nowTime);
		return $map;
	}

	/**
	 * 获取资讯中信息
	 * @param int $count
	 * @param int $mid
	 * @param int $fid
	 * @param blooean $isRand
	 * @param string $order
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function getBrandConsult($mid = 0, $fid = 0)
	{
		$map = array();
		$mmap = array();
		$mmap['m_floor_brand.mid'] = $mid;
		$mmap['m_floor_brand.fid'] = $fid;
		//获取楼层商场品牌信息
		$brands = D('FloorBrand')->getFloorBrand($mmap);
		foreach ($brands as $key => $brand) {
			unset($map);
			$map = $this->getMap($mid, $fid, $brand['bid']);
			$map = array_merge($map, $this->getStartToEndTime());
			$new = $this->getBrandNews($map, 'id,title,image,mid,fid,bid,keyword,start_time,end_time', '0,1', 'end_time ASC');
			if (count($new) == 1) {
				$brands[$key] = array_merge($brands[$key], $new[0]);
			}
		}
		return $brands;
	}

	/**
	 * 获取品牌在其他商场活动信息只获取有优惠信息的品牌
	 * @param $mid
	 * @param $fid
	 * @param $bid
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function getBrandOtherMallDis($mid, $fid, $bid)
	{
		$news = array();
		$map = array();
		$map['bid'] = $bid;
		$map['mid'] = array('neq', $mid);
		$map['fid'] = array('neq', $fid);
		$map = array_merge($map, $this->getStartToEndTime());
		$news = $this->getBrandNews($map);
		if ($news) {
			$MarketModel = D('Market');
			$FloorModel = D('Floor');
			foreach ($news as $key => $new) {
				//获取商场信息
				$news[$key]['mall'] = $MarketModel->getById($new['mid']);
				$news[$key]['ad_url'] = $this->getAdUrl($new['mid'], $new['fid'], $bid);
			}
		}
		return $news;
	}
}

?>